// pages/phone/phone.js
import showDetail from "../../../modules/showDetail";
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    slides: [],
    goods_list: [],
    navigationBarTitleText: "避孕套专场",
    keyword: '',
    pageNum: 1,
    totalPages: '',
    Authorization: '',
  },
  // showDetail(e){
  //   console.log(e.currentTarget.dataset.pid);
  // },
  showDetail,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '请稍等',
    });
    var that = this;
    var Authorization = wx.getStorageSync('Authorization');
    if(Authorization==''){
      wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
    }
    that.setData({
      Authorization: Authorization,
    })
    wx.request({
      url: "http://localhost:8080/wechat/selectgoodsbytype",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      data: {
        'pageNum': that.data.pageNum,
        'keyword': options.type,
      },
      success: function (res) {
        if (res.statusCode == 200) {
          that.setData({
            totalPages: res.data['totalPages'],
            goods_list: res.data['goods'],
            keyword: options.type,
          })
          console.info(res);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
        }
      }
    })
    wx.request({
      url: "http://localhost:8080/wechat/typead",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      data: {
        'type': options.type,
      },
      success: function (res) {
        if (res.statusCode == 200) {
          that.setData({
            slides: res.data,
          })
          console.info(that.data.slides);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
        }

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideLoading();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },

  //回到顶部
  goTop: function (e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('--------下拉刷新-------')
    var that = this;
    wx.showLoading({
      title: '请稍等',
    });
    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.request({
      url: "http://localhost:8080/wechat/selectgoodsbytype",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization': that.data.Authorization
      },
      data: {
        'pageNum': this.data.pageNum + 1,
        'keyword': this.data.keyword,
      },
      success: (res) => {
        if (res.statusCode == 200) {
          that.setData({
            pageNum: this.data.pageNum + 1,
            goods_list: this.data.goods_list.concat(res.data['goods']),
          })
          wx.hideLoading();
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
        }
      },
      fail: () => {
        // fail
      },
      complete: () => {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("向上滑动")
    //判断是否还有下一条数据
    if (this.data.pageNum >= this.data.totalPages) {
      //表示没有下一页数据
      //console.log("没有下一页数据")
      wx.showToast({
        title: '已经到底了'
      })
    } else {
      console.info(this.data.pageNum);
      console.info(this.data.totalPages);
      var Authorization = wx.getStorageSync('Authorization')
      if (Authorization == '') {
        wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
        wx.reLaunch({
          url: '/pages/openid/openid',
        })
      } else {
        this.onPullDownRefresh();
      }
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})